﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using m_pricelist.Proposal_Services;
using Microsoft.VisualBasic;
using Excel = Microsoft.Office.Interop.Excel;


namespace m_pricelist._BAL_
{
    class NewProposal_BAL : Iproposal
    {
        string fileToOpen { get; set; }
        string sFilename { get; set; }
        string sPath { get; set; }

        public void CmdPro(Proposal proposal, ProposalService ps) 
        {
            try
            {
                Excel.Application xlApp;
                xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
                if (proposal.optNewOneLang.Checked == true)
                {
                    sPath = Interaction.GetSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "NewProposal_English", Default: "Marel Proposal Template - English.docx");

                }
                else if (proposal.optNewTwoLang.Checked == true)
                {
                    sPath = Interaction.GetSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "NewProposal_Bilingual", Default: "Marel Proposal Template - bilingual.docx");
                }
                else if (proposal.optNewSLA.Checked == true)
                {
                    sPath = Interaction.GetSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "Proposal_SLA", Default: "SLA - English.docx");
                }

                sFilename = ps.GetFilenameFromPath(sPath);

                if (sPath == "")
                {
                    FileSystem.ChDir(xlApp.Path);
                }
                else
                {
                    FileSystem.ChDir(sPath.Replace(sFilename, ""));
                }

                OpenFileDialog openFile = new OpenFileDialog();
                openFile.InitialDirectory = @"C:\LocalData\Marel\Marel Pricelist 2H 2013\Marel Proposal";

                openFile.Title = "Please select a proposal";
                openFile.Filter = "Word files (*.docx)|*.docx|All Files(*.*)|*.*";
                DialogResult result = openFile.ShowDialog();
                fileToOpen = openFile.FileName.ToString();

                if (result == DialogResult.OK) // Test result.
                {
                    if (proposal.optNewOneLang.Checked == true)
                        Interaction.SaveSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "NewProposal_English", Setting: fileToOpen);
                    else if (proposal.optNewTwoLang.Checked == true)
                        Interaction.SaveSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "NewProposal_Bilingual", Setting: fileToOpen);
                    else if (proposal.optNewSLA.Checked == true)
                        Interaction.SaveSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "Proposal_SLA", Setting: fileToOpen);
                }
                else
                {
                    //sPath = Interaction.GetSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "Proposal_English"); //ActiveWorkbook.PathsP
                    MessageBox.Show("No other template has been selected");
                }
            }

            catch (Exception ex)
            {
                ps.CleanUp(string.Empty);
                MessageBox.Show(ex.ToString());
            }
 
        }

        public void cmdProposal(Proposal proposal, bNewProposal bNew, ref Excel.Worksheet xlsheet) 
        {
            proposal = new Proposal();
            bNew = new bNewProposal();
            object sFileToOpen = "";
            object sPath;

            try
            {
                if (proposal.optNewOneLang.Checked == true)
                {
                    sPath = Interaction.GetSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "NewProposal_English");
                    bNew.GetbNewProposal(proposal, sPath, sFileToOpen, ref xlsheet);
                }

                else if (proposal.optNewTwoLang.Checked == true)
                {
                    sPath = Interaction.GetSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "NewProposal_Bilingual");
                    bNew.GetbNewProposal(proposal, sPath, sFileToOpen, ref xlsheet);
                    MessageBox.Show(sPath.ToString());
                }
                else if (proposal.optNewSLA.Checked == true)
                {
                    sPath = Interaction.GetSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "Proposal_SLA");
                    //sla.GetSLAProposal(sPath, sFileToOpen, wdApp, xlApp, xlSheet, xlWorkBook);
                    MessageBox.Show(sPath.ToString());
                }
            }

            catch (Exception ex)
            {
                //Drepa allt
                //ps.CleanUp(string.Empty, wdApp);
                MessageBox.Show(ex.ToString());
            }
            
        }

        public void cmdUproposal(Proposal proposal) 
        {
            proposal = new Proposal();
            object fileToOpen = null;
            object sPath = null;
            string sOldDir = null;
            object InitialPath = @"C:\LocalData\Marel\Marel Pricelist 2H 2013\Marel Proposal";
            Excel.Application app;
            app = (Excel.Application)Marshal.GetActiveObject("Excel.Application");

            sPath = InitialPath;
            sOldDir = System.IO.Directory.GetCurrentDirectory();

            FileSystem.ChDrive(sPath.ToString());
            FileSystem.ChDir(sPath.ToString());

            if (proposal.optNewOneLang.Checked == true)
            {
                fileToOpen = app.GetOpenFilename(FileFilter: "Marel Proposal Template  - English, *.docx* ", Title: "Please select a Proposal");
            }
            else if (proposal.optNewTwoLang.Checked == true)
            {
                fileToOpen = app.GetOpenFilename(FileFilter: "Marel Proposal Template  - bilingual.docx, *.docx* ", Title: "Please select a Proposal");
            }

            else if (proposal.optNewSLA.Checked == true)
            {
                fileToOpen = app.GetOpenFilename(FileFilter: "SLA - English.docx, *.docx* ", Title: "Please select a Proposal");
            }

            try
            {
                FileSystem.ChDrive(sOldDir);
                FileSystem.ChDir(sOldDir);

                if (fileToOpen.Equals(false))
                {
                    MessageBox.Show("No document was selected");
                }
                else
                {
                    //Þarf að hafa einhverskonar villumeðhöndulun
                    if (proposal.optNewSLA.Checked == true)
                    {
                        //sla.GetSLAProposal(sPath, fileToOpen, wdApp, xlApp, xlSheet, xlWorkBook);
                        MessageBox.Show("GetSLAProposal");
                    }
                    else
                    {
                        //bNew.GetbNewProposal(sPath, fileToOpen, xlSheet, wdApp, xlWorkBookObjects, xlWorkBook);
                        MessageBox.Show("GetbNewProposal");
                    }
                }
            }
            catch (Exception ex)
            {
               // ps.CleanUp(string.Empty, wdApp);
                MessageBox.Show(ex.ToString() + "An error came up ", "@\n" + ex);
            }
        }

        
    }
}
